import {jsonp} from "vue-jsonp";
import { MD5 } from "@/utils/MD5";

export const callBaiduAPI = (text: string, index: number, targetField: 'chinese' | 'english') => {
    return new Promise((resolve, reject) => {
        var appid = '20240828002135064';
        var key = 'RmzDASXXXEc4bTFy7dOX';
        var salt = (new Date).getTime();
        var query = text;
        var from = targetField === 'chinese' ? 'en' : 'zh';
        var to = targetField === 'chinese' ? 'zh' : 'en';
        console.log(from + '_' + to)
        var data = ""

        var str1 = appid + query + salt + key;
        var sign = MD5(str1);
        jsonp('http://api.fanyi.baidu.com/api/trans/vip/translate', {
            q: query,
            appid: appid,
            salt: salt,
            from: from,
            to: to,
            sign: sign
        }).then(response => {
            console.log(response)
            data = response['trans_result'][0]['dst'];
            console.log(data)
            resolve(data);
        }).catch(error => {
            console.error(error);
            reject(error);
        });
    });
}

export default callBaiduAPI;